// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:13:09
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Constraint\RBAR1Parser.cpp
 */

#include "Import/BDFImport/BulkData/Constraint/RBAR1Parser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <unordered_set>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRBAR1Service.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (RBAR1);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/RBAR1/RBAR1.md>
*/
enum RBAR1Key : std::uint8_t
{
    RBAR1Key_EID   = 2,
    RBAR1Key_GA    = 3,
    RBAR1Key_GB    = 4,
    RBAR1Key_CB    = 5,  // Component numbers in the global coordinate system at GB, which are constrained to move as the rigid bar.
    RBAR1Key_ALPHA = 6,
    RBAR1Key_TREF  = 7,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseRBAR1 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::RBAR1Data tempData;
    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId  = 1;
    tempData.m_slaveDOFs = 123456;  // default CB = “123456”

    int curIDKey = 1;               // 当前字符串是第几项

    for (auto curStrKey : dividedEntryInfo)
    {
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        switch (curIDKey)
        {
        case RBAR1Key_EID:
            tempData.m_id = convertToI64 (curStrKey);
            break;
        case RBAR1Key_GA:
            tempData.m_masterNode = convertToI64 (curStrKey);
            break;
        case RBAR1Key_GB:
            tempData.m_slaveNodes = convertToI64 (curStrKey);
            break;
        case RBAR1Key_CB:
            tempData.m_slaveDOFs = convertToI64 (curStrKey);
            break;
        case RBAR1Key_ALPHA:
            tempData.m_alpha = convertToReal (curStrKey);
            break;
        case RBAR1Key_TREF:
            tempData.m_refTemperature = convertToReal (curStrKey);
            break;
        default:
            break;
        }
        curIDKey++;
    }

    return dbServiceFactory->get<IRBAR1Service> ()->Add (tempData);
}
